import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt

# 加载数据集
data = pd.read_csv("src\\regression.csv")

# 删除有缺失的样本
data.dropna(inplace=True)

# 提取特征和标签
X = data.iloc[:, :-1]  # 特征
y = data.iloc[:, -1]   # 标签

# 划分数据集，80%训练，20%测试
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 设置不同的C值进行实验
C_values = [0.1, 1.0, 10.0]
epsilon_values = [0.01, 0.1, 1.0]

results = []

for C in C_values:
    for epsilon in epsilon_values:
        # 使用支持向量机回归（SVR）模型
        model = SVR(kernel='rbf', C=C, epsilon=epsilon)
        model.fit(X_train, y_train)

        # 预测和评估
        y_pred = model.predict(X_test)
        mse = mean_squared_error(y_test, y_pred)
        
        # 保存结果
        results.append({
            'C': C,
            'epsilon': epsilon,
            'MSE': mse
        })
        print(f"C: {C}, Epsilon: {epsilon}, Mean Squared Error (MSE): {mse}")

# 转换结果为DataFrame
results_df = pd.DataFrame(results)

# 结果可视化
plt.figure(figsize=(10, 6))
for C in C_values:
    subset = results_df[results_df['C'] == C]
    plt.plot(subset['epsilon'], subset['MSE'], marker='o', label=f'C={C}')

plt.title('SVM Regression MSE vs Epsilon for Different C values')
plt.xlabel('Epsilon')
plt.ylabel('Mean Squared Error')
plt.legend()
plt.grid(True)
plt.show()
